package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.ValueChangeEvent;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ResourceDAO;
import com.erpanderp.wam.util.CommonLookup;

@ManagedBean
@ViewScoped
public class ResourcesBean implements Serializable {
	private static final long serialVersionUID = 1L;

	private Map<String, Long> modulesMap = new HashMap<String, Long>();
	private Map<String, Long> resourcesMap = new HashMap<String, Long>();

	ModuleDAO moduleDAO = new ModuleDAO();
	ResourceDAO resourceDAO = new ResourceDAO();
	
	private List<Resource> resources = new ArrayList<Resource>();
	
	private Resource resource = new Resource();

	private Long selectedModuleId;
	private Long selectedResourceId;
	private Resource selectedResource;
	
	private HtmlSelectOneMenu moduleSelectOneMenu;

	public ResourcesBean() {
		List<Module> modules = moduleDAO.getAllModules();
		for (Module module : modules) {
			modulesMap.put(module.getModuleName(), module.getModuleId());
		}

	}

	public void moduleChangeListener(AjaxBehaviorEvent e) {
		resourcesMap.clear();
		if(selectedModuleId == null || selectedModuleId == 0)
		{
		
		}
		else
		{
		resources = resourceDAO.getResources(selectedModuleId);

		for (Resource resource : resources) {
			resourcesMap.put(resource.getResourceName(),
					resource.getResourceId());
		}

		
		}
	}
	public void resourceChangeListener(AjaxBehaviorEvent e1) {
	
		System.out.println("resourceChangeListener..........");
		if(selectedResourceId == null || selectedResourceId == 0)
		{
		
		}
		else
		{
			selectedResource = resourceDAO.getResourceById(selectedResourceId);
			System.out.println("selectedResource........"+selectedResource);

		}
		
	}
	
	
	public String updateResourceAction() {
		
		System.out.println("updateResourceAction.............");
		resourceDAO.updateResource(selectedResource);
		return null;
	}

	// GETTERS AND SETTERS

	public Map<String, Long> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, Long> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public Map<String, Long> getResourcesMap() {
		return resourcesMap;
	}

	public void setResourcesMap(Map<String, Long> resourcesMap) {
		this.resourcesMap = resourcesMap;
	}

	public Long getSelectedModuleId() {
		return selectedModuleId;
	}

	public void setSelectedModuleId(Long selectedModuleId) {
		this.selectedModuleId = selectedModuleId;
	}

	public HtmlSelectOneMenu getModuleSelectOneMenu() {
		return moduleSelectOneMenu;
	}

	public void setModuleSelectOneMenu(HtmlSelectOneMenu moduleSelectOneMenu) {
		this.moduleSelectOneMenu = moduleSelectOneMenu;
	}

	public Resource getSelectedResource() {
		return selectedResource;
	}

	public void setSelectedResource(Resource selectedResource) {
		this.selectedResource = selectedResource;
	}

	public Long getSelectedResourceId() {
		return selectedResourceId;
	}

	public void setSelectedResourceId(Long selectedResourceId) {
		this.selectedResourceId = selectedResourceId;
	}

	public Resource getResource() {
		return resource;
	}

	public void setResource(Resource resource) {
		this.resource = resource;
	}

	

	


	
}
